<?php


namespace app\logic;

use think\facade\Db;

class OrderLogic
{
    public function successOrderCallBack($data)
    {
        $info = Db::name('order')->where('order_no', $data['out_trade_no'])->find();
        if ($info != false && $data['result_code'] == 'SUCCESS' && $data['sign_status'] == 1) {
            $updateData['type'] = 2;
            $updateData['bank_type'] = $data['bank_type'];
            $updateData['fee_type'] = $data['fee_type'];
            $updateData['openid'] = $data['openid'];
            $updateData['pay_time'] = $data['time_end'];
            $result = Db::name('order')->where('order_no', $data['out_trade_no'])->update($updateData);
            if ($result != false) {
                $this->userOrderAddTime($data['out_trade_no']);
            }
            return $result != false ? true : false;
        } else {
            return false;
        }
    }

    public function successAliOrderCallBack($data)
    {
        $info = Db::name('order')->where('order_no', $data['out_trade_no'])->find();
        if ($info != false && $data['trade_status'] == 'TRADE_SUCCESS' && $data['sign_status'] == 1) {
            $updateData['type'] = 2;
            $updateData['bank_type'] = 1;
            $updateData['fee_type'] = $data['buyer_pay_amount'];
            $updateData['openid'] = $data['buyer_id'];
            $updateData['pay_time'] = $data['gmt_payment'];
            $result = Db::name('order')->where('order_no', $data['out_trade_no'])->update($updateData);
            if ($result != false) {
                $this->userOrderAddTime($data['out_trade_no']);
            }
            return $result != false ? 'success' : 'fail';
        } else {
            return 'fail';
        }
    }

    /**
     * 给用户增加时间
     */
    public function userOrderAddTime($order_no)
    {
        $info = Db::name('order')->alias('o')
            ->field('u.pay_time,u.how_long_time,u.id,o.pay_time as o_pay_time,c.how_long_time as c_how_long_time,o.search_pay_type,o.num as o_num,c.num as c_num,o.id as oid')
            ->leftJoin('user u', 'u.id=o.user_id')
            ->leftJoin('combo c', 'c.id=o.combo_id')
            ->where('o.type', 2)->where('o.is_used', 1)->where('o.order_no', $order_no)->find();

        if ($info != false) {
            Db::startTrans();
            try {
                Db::name('order')->where('id', $info['oid'])->update(['is_used' => 2, 'used_time' => toDate(time())]);
                $isPayCount = Db::name('order')->where('user_id', $info['id'])->where('type', 2)->count();
                if ($isPayCount == 1) {//--第一支付
                    if ($info['pay_time'] > 0) {
                        $data['how_long_time'] = $info['how_long_time'] + $info['c_how_long_time'];
                    } else {
                        $data['pay_time'] = strtotime($info['o_pay_time']);
                        $data['how_long_time'] = $info['c_how_long_time'];
                    }
                    $info['c_num'] = $info['c_num'] > 0 ? $info['c_num'] - 1 : $info['c_num'];
                } else {
                    $data['how_long_time'] = $info['c_how_long_time'] + $info['how_long_time'];
                }

                $type = $info['search_pay_type'];

                if ($type == 1) {//为本账号续费
                    $info['c_num'] = 0;
                }
                if ($type == 2) {//添加新账号
                    unset($data['how_long_time']);
                    $info['c_num'] = $info['o_num'];
                }
                if ($type == 3) {//在该账号下添加6个账号
                    unset($data['how_long_time']);
                }
                if ($type == 4) {//在该账号下添加5个账号为自己续费一年
                    $info['c_num'] = $info['c_num'] - 1;
                }

                if (!empty($data)) {
                    Db::name('user')->where('id', $info['id'])->update($data);
                }
                if ($info['c_num'] > 0) {
                    Db::name('user')->where('id', $info['id'])->inc('max_num', $info['c_num'])->update();
                }
                Db::commit();
                $this->addAccount($info['id']);
            } catch (\Exception $e) {
                Db::rollback();
            }
        }
    }

    public function addAccount($id)
    {
        $count = Db::name('user')->where('pid', $id)->count();
        $info = Db::name('user')->where('id', $id)->find();
        $orderInfo = Db::name('order')->where('type',2)->where('user_id',$info['id'])->where('is_used',2)->order('used_time','desc')->find();
        $data = [];
        for ($i = 0; $i < $info['max_num']; $i++) {
            $px = $count + $i;
            $data[] = ['login_name' => $info['login_name'] . 'c' . $px,
                'password' => '670b14728ad9902aecba32e22fa4f6bd',
                'company_name' => $info['company_name'],
                'industry' => $info['industry'],
                'address' => $info['address'],
                'main_product' => $info['main_product'],
                'pay_time' => strtotime($orderInfo['used_time']),
                'how_long_time' => 86400 * 365,
                'max_num' => 0,
                'pid' => $id,
            ];
        }
        Db::startTrans();
        try{
            Db::name('user')->insertAll($data);
            Db::name('user')->where('id', $info['id'])->dec('max_num', count($data))->update();
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
        }

    }

}